var express = require('express');        // call express
var app = express();                 // 获得express定义的app，app对象此时代表整个web应用
var bodyParser = require('body-parser');

// 给app配置bodyParser中间件
// 通过如下配置再路由种处理request时，可以直接获得post请求的body部分
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//设置跨域访问
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", '*'); //需要显示设置来源
    res.header("Access-Control-Allow-Headers", req.headers['access-control-request-headers']);
    // res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    // res.header("Access-Control-Allow-Credentials",true); //带cookies
    res.header("X-Powered-By", ' 3.2.1')
    // res.header("Content-Type", "application/json;charset=utf-8");

    if (req.method === 'OPTIONS') {
        res.send(200, '');
    } else {
        next();
    }
});
var port = process.env.PORT || 9080;        // set our port

/*路由分发*/
var routes = require('./src/router');
/*路由*/
routes(app);

// 启动server
// =============================================================================
//开始监听端口
app.listen(port);
console.log('http://localhost:' + port);